package com.hifar.log;

import com.hifar.config.SpringContextUtils;
import com.hifar.plat.baseModal.service.IBaseSysLogService;
import com.hifar.plat.util.IpUtils;
import com.hifar.sys.SystemLoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhuWeiWei
 * @description TODO
 * @date 2025/5/13 9:28
 */
@Component
public class AutoLogUtils {

    @Autowired
    private IBaseSysLogService baseSysLogService;


    public void addLog(String title, String content) {
        String ip = getIp();
        String userId = SystemLoginUser.userId;
        String userName = SystemLoginUser.idName;
        long tm = System.currentTimeMillis();
        baseSysLogService.addLogRecord(title, content, userId, userName, ip, true, "", tm);
    }

    public void exportLog(String title, String content) {
        String ip = getIp();
        String userId = SystemLoginUser.userId;
        String userName = SystemLoginUser.idName;
        long tm = System.currentTimeMillis();
        baseSysLogService.exportLogRecord(title, content, userId, userName, ip, true, "", tm);
    }

    public void importLog(String title, String content) {
        String ip = getIp();
        String userId = SystemLoginUser.userId;
        String userName = SystemLoginUser.idName;
        long tm = System.currentTimeMillis();
        baseSysLogService.importLogRecord(title, content, userId, userName, ip, true, "", tm);
    }


    public void modifyLog(String title, String content) {
        String userId = SystemLoginUser.userId;
        String userName = SystemLoginUser.idName;
        String ip = getIp();
        long tm = System.currentTimeMillis();
        baseSysLogService.modifyLogRecord(title, content, userId, userName, ip, true, "", tm);
    }


    public void deleteLog(String title, String content) {
        String userId = SystemLoginUser.userId;
        String userName = SystemLoginUser.idName;
        String ip = getIp();
        long tm = System.currentTimeMillis();
        baseSysLogService.deleteLogRecord(title, content, userId, userName, ip, true, "", tm);
    }


    private String getIp() {
        HttpServletRequest request = null;
        String ip = "127.0.0.1";
        try {
            //获取request
            request = SpringContextUtils.getHttpServletRequest();
            ip = IpUtils.getIpAddress(request);
        } catch (Exception e) {
        }
        return ip;
    }


}
